package com.gaoyi.crm.activity.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.gaoyi.crm.R;
import com.gaoyi.crm.constant.Constants;
import com.gaoyi.crm.constant.EventCode;
import com.gaoyi.crm.constant.SPName;
import com.gaoyi.crm.control.popup.MyPopupWindow;
import com.gaoyi.crm.databinding.MyActionBarBinding;
import com.gaoyi.crm.db.bean.Event;
import com.gaoyi.crm.util.ActivityUtil;
import com.gaoyi.crm.util.DensityUtil;
import com.gaoyi.crm.util.LanguageUtils;
import com.gaoyi.crm.util.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.Map;

public abstract class BasePermissionActivity extends AppCompatActivity {

    protected ActivityResultLauncher<String[]> mPermissionsResult;
    private WeakReference<AppCompatActivity> mWeakReference = null;
    private MyPopupWindow mPopup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActionBar();
        EventBus.getDefault().register(this);
        if (mWeakReference == null) {
            mWeakReference = new WeakReference<>(this);
        }
        ActivityUtil.getInstance().addActivity(mWeakReference.get());
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            int x = DensityUtil.dp2px(20);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.my_action_bar);
            MyActionBarBinding binding = MyActionBarBinding.bind(actionBar.getCustomView());
            if (!SPUtil.getInstance().getString(SPName.TOKEN).isEmpty()) {
                binding.tvUser.setText(SPUtil.getInstance().getString(SPName.USER));
            }
            binding.tvUser.setOnEndClickListener(() -> showPopupWindow(binding,x));
        }
    }

    private void showPopupWindow(MyActionBarBinding binding,int x){
        if (mPopup == null) {
            mPopup = new MyPopupWindow(this,()-> ActivityUtil.getInstance().finishActivity());
        }
        mPopup.setNetState(SPUtil.getInstance().getInt(SPName.NET_STATE, Constants.ONLINE));
        if (mPopup.isShowing()) {
            mPopup.dismiss();
        }else {
            mPopup.showAsDropDown(binding.tvUser, x, 0, Gravity.END);
        }
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.getInstance().change(LanguageUtils.getInstance().getCurrentLanguage()));
    }

    protected abstract void event(Event event);

    protected abstract void onRequestPermissionsResult(Map<String, Boolean> result);

    protected abstract void onActivityResult(int requestCode, ActivityResult result);

    protected ActivityResultLauncher<Intent> registerForActivityResult(int requestCode) {
        return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> onActivityResult(requestCode, result));
    }

    protected ActivityResultLauncher<String[]> registerForPermissionsResult() {
        return registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), this::onRequestPermissionsResult);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_LANGUAGE) {
            recreate();
        } else {
            event(event);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.getInstance().removeActivity(mWeakReference.get());
        ActivityUtil.getInstance().removeFragment(mWeakReference.get());
    }
}
